cur_dir=`pwd`
base_url=jp2.php.net
base_url1=am1.php.net
base_url2=cn2.php.net
php_version=7.1.11
url=http://${base_url}/distributions/php-${php_version}.tar.bz2
url1=http://${base_url1}/distributions/php-${php_version}.tar.bz2
url2=http://${base_url2}/distributions/php-${php_version}.tar.bz2

Color_Text()
{
  echo -e " \e[0;$2m$1\e[0m"
}

Echo_Red()
{
  echo $(Color_Text "$1" "31")
}

Echo_Green()
{
  echo $(Color_Text "$1" "32")
}

Echo_Yellow()
{
  echo -n $(Color_Text "$1" "33")
}

Echo_Blue()
{
  echo $(Color_Text "$1" "34")
}
Fail_Stop()
{
    if [ $? -ne 0 ]; then
        Echo_Red "FOUND ERROR, STOP!"
        exit 1
    fi
}
Try_Make()
{
    make
    Fail_Stop
    make install
    Fail_Stop
}

CentOS_Dependent()
{
    for packages in make cmake gcc gcc-c++ flex bison file libtool libtool-libs autoconf kernel-devel patch wget crontabs libjpeg libjpeg-devel libpng libpng-devel gd gd-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel unzip tar bzip2 bzip2-devel libzip-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel libcurl libcurl-devel e2fsprogs e2fsprogs-devel libidn libidn-devel openssl openssl-devel vim-minimal gettext gettext-devel ncurses-devel gmp-devel unzip libcap diffutils ca-certificates net-tools libc-client-devel psmisc libXpm-devel git-core c-ares-devel libicu-devel libxslt libxslt-devel xz expat-devel libaio-devel libtirpc-devel perl mcrypt libmcrypt libmcrypt-devel libmemcached libmemcached-devel;
    do
        yum -y install $packages;
        Fail_Stop
    done
}

Download_PHP()
{
    if [ -s php-${php_version}.tar.bz2 ]; then
        echo "php-${php_version}.tar.bz2 [found]"
    else
        echo "Notice: php-$php_version.tar.bz2 not found!!!download now..."
        cd ${cur_dir}
        wget -c --progress=bar:force ${url}
        if [ $? -ne 0 ]; then
            wget -c --progress=bar:force ${url1}
        fi
        if [ $? -ne 0 ]; then
            wget -c --progress=bar:force ${url2}
        fi
        if [ $? -eq 0 ]; then
            echo "Download php-${php_version}.tar.bz2 successfully!"
        else
            echo "You enter PHP Version was:"${php_version}
            Echo_Red "Error! You entered a wrong version number, please check!"
            exit 1
        fi
    fi
}

Tarj_Cd()
{
    local FileName=$1
    local DirName=$2
    cd ${cur_dir}
    [[ -d "${DirName}" ]] && rm -rf ${DirName}
    echo "Uncompress ${FileName}..."
    tar jxf ${FileName}
    echo "cd ${DirName}..."
    cd ${DirName}
}

Install_PHP_71()
{
    Echo_Blue "[+] Installing php-${php_version}"
    Tarj_Cd php-${php_version}.tar.bz2 php-${php_version}
    shell="./configure '--prefix=/opt/php7' '--with-config-file-path=/opt/php7/etc'  --with-config-file-scan-dir=/opt/php7/etc/conf.d '--enable-fpm' '--with-fpm-user=www' '--with-fpm-group=www' '--enable-inline-optimization' '--disable-debug' '--disable-rpath' '--with-openssl' '--with-mcrypt' '--with-mhash' '--with-curl' '--with-gd' '--with-openssl-dir' '--with-jpeg-dir' '--with-png-dir' '--enable-json' '--enable-mbstring' '--enable-pdo' '--with-pdo-mysql=mysqlnd' '--with-zlib-dir'  --enable-pcntl  '--enable-sockets' '--with-libxml-dir' '--enable-zip' '--enable-mysqlnd-compression-support' '--enable-opcache' '--enable-xml' '--enable-mysqlnd'"

    echo $shell
    eval $shell
    Fail_Stop

    Try_Make

    cd ${cur_dir}
}

Ln_PHP_Bin()
{
    ln -sf /opt/php7/bin/php /usr/bin/php
    ln -sf /opt/php7/bin/phpize /usr/bin/phpize
    ln -sf /opt/php7/bin/php-config /usr/bin/php-config
    ln -sf /opt/php7/bin/pear /usr/bin/pear
    ln -sf /opt/php7/bin/pecl /usr/bin/pecl
    ln -sf /opt/php7/sbin/php-fpm /usr/bin/php-fpm
}
PHP_EXT_INSTALL()
{
    mkdir -p exts && cd exts

    PECL_INSTALL
    MEMCACHE_INSTALL
    MOLTEN_INSTALL

    cd ${cur_dir}
}

PECL_INSTALL()
{
    pecl channel-update pecl.php.net

    for packages in igbinary-2.0.7  yaconf-1.0.7 yac-2.0.2 memcached-3.0.4  redis-3.1.4 apcu-5.1.12
    do
        if [ -s ${packages}.tgz ]; then
            echo "${packages}.tgz found ";
        else
            pecl download $packages;
        fi
    done

    for packages in igbinary-2.0.7  yaconf-1.0.7 yac-2.0.2 memcached-3.0.4  redis-3.1.4 apcu-5.1.12
    do
        tar xvf ${packages}.tgz
        cd ${packages}
        /usr/bin/phpize
        ./configure

        Try_Make
        cd ../
    done
}

UNZIP_PHPIZE()
{
    file_name=$1
    dir_name=$2
    if [ -s ${dir_name} ]; then
        echo "${dir_name} [found]"
    else
        unzip ${file_name}
    fi
    cd ${dir_name}
    /usr/bin/phpize
    ./configure
    Try_Make

    cd ../
}

MEMCACHE_INSTALL()
{
    file_name=php-memcache-NON_BLOCKING_IO_php7.zip
    dir_name=pecl-memcache-NON_BLOCKING_IO_php7

    if [ -s ${file_name} ]; then
        echo "${file_name} [found]"
    else
        memcache_url=https://github.com/websupport-sk/pecl-memcache/archive/NON_BLOCKING_IO_php7.zip
        wget -c --progress=bar:force ${memcache_url} -O ${file_name}

        if [ $? -eq 0 ]; then
            echo "Download ${file_name} successfully!"
        else
            echo "You php-memcache url "${memcache_url}
            Echo_Red "Error! php-memcache download fail!"
            exit 1
        fi
    fi
    UNZIP_PHPIZE ${file_name} ${dir_name}
}

MOLTEN_INSTALL()
{
    file_name=php-molten.zip
    dir_name=Molten-master
    if [ -s ${file_name} ]; then
        echo "${file_name} [found]"
    else
        molten_url=https://github.com/chuan-yun/Molten/archive/master.zip
        wget -c --progress=bar:force ${molten_url} -O ${file_name}

        if [ $? -eq 0 ]; then
            echo "Download ${file_name} successfully!"
        else
            echo "You ${file_name} "${molten_url}
            Echo_Red "Error! ${file_name} download fail!"
            exit 1
        fi
    fi
    UNZIP_PHPIZE ${file_name} ${dir_name}
}

PHP_CONFIG_INI()
{
    cd ${cur_dir}
    cp -rf php.ini /opt/php7/etc/
    cp -rf conf.d /opt/php7/etc/
    cp -rf php-fpm.conf /opt/php7/etc/

}

PHP_EXT_INSTALL
PHP_CONFIG_INI
